package com.fingard.dsp.bank.directbank.cib02;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.bankInterface.IFinanceRecInfo;
import com.fingard.dsp.bank.directbank.fngrd01.util.StringHelper;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.ReqQryFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfo;
import com.fingard.dsp.bank.reqrespobj.qryfinancerecinfo.RetAtsFinanceRecInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 获取还款信息
 *
 * @author Parker on 2019-11-19.
 * @version 1.0
 */
public class CIB02QryFinanceRecInfo extends CIB02Base implements IFinanceRecInfo {
    @Override
    public RetAtsFinanceRecInfo qryFinanceRecInfo(ReqQryFinanceRecInfo recInfo) throws Exception {
        RetAtsFinanceRecInfo retAts = new RetAtsFinanceRecInfo(recInfo.reqHeader);
        retAts.writeStart();
        String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            writeCommonHeader(xmlWriter);
            xmlWriter.writeStartElement("REQUEST");
            xmlWriter.writeElementString("TRANCODE", "18002");//交易代码
            xmlWriter.writeElementString("COREID", bankFront.org);//直联企业系统客户号
            xmlWriter.writeElementString("COTYPE", getBankConfig().getValueAsString("coType"));//对接企业类型 1核心企业2服务商
            xmlWriter.writeElementString("SCOCOD", "");//核心企业组织机构代码
            xmlWriter.writeElementString("DLENUM", recInfo.DealerCode);//经销商代码
            xmlWriter.writeElementString("STADAT", "");//赎证日期起
            xmlWriter.writeElementString("ENDDAT", "");//赎证日期止
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();

            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),"T18002");//发送银行

            if (tmpStrRet[0].length() == 0) {
                StringBuilder tmpSbMsg = new StringBuilder();
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if (tmpBkReader != null) {
                    String tmpRetCode = tmpBkReader.getFirstText("/RESPONSE/RTNCOD");
                    String tmpRetMsg = tmpBkReader.getFirstText("/RESPONSE/RTNMSG");
                    if (StringHelper.isNullOrEmpty(tmpRetCode)) {
                        tmpRespCode = "0";
                        tmpRespInfo = "获取成功";
                        RetAtsFinanceRecInfoDetail tmpRetDetail = new RetAtsFinanceRecInfoDetail();
                        tmpRetDetail.DealerCode = recInfo.DealerCode;
                        tmpRetDetail.VIN = tmpBkReader.getFirstText("/RESPONSE/CHANUM");
                        tmpRetDetail.RepaymentDate = Format.DateTimeFormat.format(Format.Date10Format.parse(tmpBkReader.getFirstText("/RESPONSE/DECDAT")));
                        tmpRetDetail.RepaymentMoney = tmpBkReader.getFirstText("/RESPONSE/CRRAMT");
                        String CarMortgageTime = tmpBkReader.getFirstText("/RESPONSE/ENTDAT");
                        if (StringHelper.isNullOrEmpty(CarMortgageTime)) {
                            tmpRetDetail.CarMortgageTime = "";
                        } else {
                            tmpRetDetail.CarMortgageTime = Format.DateTimeFormat.format(Format.Date10Format.parse(CarMortgageTime));
                        }
                        tmpRetDetail.Reserve1 = "";
                        tmpRetDetail.Reserve2 = "";
                        tmpRetDetail.Reserve3 = "";
                        tmpRetDetail.Reserve4 = "";
                        tmpRetDetail.Reserve5 = "";
                        retAts.writeRetDetail(tmpRetDetail);
                    } else {
                        tmpRespCode = "-1";
                        tmpRespInfo = tmpRetMsg;
                    }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }
}
